import {
  GetCaptcha,
  GetCaptchaVerify,
  GetUserLogin,
  GetUserLoginRefresh,
} from "apis/index";
import { regexp, showToast } from "utils/index";
const seconds = 59;
const app = getApp();
const map = {
  password: {
    title: "账号密码登录",
    tips: "",
  },
  qrcode: {
    title: "二维码登录",
    tips: "",
  },
  captcha: {
    title: "短信验证登录",
    tips: "",
  },
};
Page({
  data: {
    map,
    loginType: "captcha", //password,qrcode,captcha,
    isAgree: false, //是否同意协议
    showPopup: false,
    phone: "18483221518", //手机号
    captcha: "", //验证码
    password: "", //密码
    countNum: seconds,
    isSendCaptcha: false, //是否发送了验证码
  },

  onLoad(options) {
    const { type = "captcha" } = options;
  },

  onShareAppMessage() {},
  //切换是否同意协议
  toggleAgree(e) {
    this.setData({
      isAgree: e.detail,
    });
  },
  //登录
  onLogin() {
    const { phone, captcha, password, isAgree, loginType } = this.data;
    // GetCaptchaVerify({ phone, captcha }).then((res) => {
    //   console.log(phone, captcha, res, "res---验证验证码");
    const params = { phone };
    let isValid = false;
    if (loginType === "password") {
      params.password = password;
      isValid = this.checkPhone() && this.checkPsd();
    } else if (loginType === "captcha") {
      params.captcha = captcha;
      isValid = this.checkPhone() && this.checkCaptcha();
    }
    if (!isValid) {
      return;
    }
    if (!isAgree) {
      this.setData({
        showPopup: true,
      });
      return;
    }
    console.log("登录类型及参数-------------", loginType, params);
    // GetUserLogin(params).then((res) => {
    //   console.log("登录成功-res---------", res);
    const info = {
      phone: "18483221518",
      nickname: "别样的感动",
      avatar:
        "https://p4.music.126.net/SzJI45iUYD-qec2qkSdZmQ==/109951168230759299.jpg",
      isVip: false,
    };
    app.globalData.userInfo = info;
    wx.setStorageSync("userInfo", info);
    wx.setStorageSync("token", "1234567890");
    wx.navigateBack();
    // });
  },
  //获取用户信息
  getUserInfo() {
    GetUserInfo({ uid: "" }).then((res) => {
      console.log("userInfo------------", res);
    });
  },
  //当输入框输入时
  onInput(e) {
    const { name } = e.currentTarget.dataset;
    const { value } = e.detail;
    this.setData({
      [name]: value,
    });
  },
  //弹窗中同意协议
  onConfirmProtocol() {
    this.setData({
      isAgree: true,
      showPopup: false,
    });
  },
  //检查电话号码
  checkPhone() {
    const { phone } = this.data;
    if (!phone) {
      showToast("请输入电话号码");
      return false;
    }
    if (!regexp.phone_11.test(phone)) {
      showToast("请输入正确的11位电话号码");
      return false;
    }
    return true;
  },
  //检查密码
  checkPsd() {
    const { password } = this.data;
    if (!password) {
      showToast("请输入密码");
      return false;
    }
    if (!regexp.password.test(password)) {
      showToast("请输入正确的16位密码");
      return false;
    }
    return true;
  },
  //检查验证码
  checkCaptcha() {
    const { captcha } = this.data;
    if (!captcha) {
      showToast("请输入验证码");
      return false;
    }
    return true;
  },
  //获取验证码
  getCaptcha() {
    const { phone, countNum, isSendCaptcha } = this.data;
    if (!this.checkPhone()) return;
    if (isSendCaptcha && !countNum < seconds) {
      showToast("已发送，请稍后再试");
      return;
    }
    this.setData({
      isSendCaptcha: true,
    });
    GetCaptcha({ phone })
      .then((res) => {
        const timer = setInterval(() => {
          const { countNum } = this.data;
          this.setData({
            countNum: countNum - 1,
          });
          if (countNum <= 1) {
            clearInterval(timer);
            this.setData({
              countNum: seconds,
              isSendCaptcha: false,
            });
          }
        }, 1000);
      })
      .catch(() => {
        this.setData({
          countNum: seconds,
          isSendCaptcha: false,
        });
      });
  },
  //改变登录方式
  changeLoginType(e) {
    const { type } = e.currentTarget.dataset;
    this.setData({
      loginType: type,
    });
  },
});
